﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using Point = System.Drawing.Point;
using System.Windows.Forms;

using DotSpatial.Data;
using DotSpatial.Controls;

using DotSpatial.Topology;



namespace HaGisPack
{
    public class RectangleMF2 : FMapFunctionBase
    {

        # region 构造函数

        public RectangleMF2(IMap map)
            : base(map)
        {
            Configure();

        }
        public RectangleMF2()
        {

            Configure();

        }

        private void Configure()
        {
            _parts = new List<List<Coordinate>>();
        }


        # endregion


        #region private variables

        private bool _standBy;


        private List<Coordinate> _coordinates;
        private List<List<Coordinate>> _parts;
        private System.Drawing.Point _mousePosition;

        #endregion





        # region 方法


        /// <summary>
        /// 开始进行自我操作的部分
        /// </summary>
        protected override void OnDeactivate()
        {
            if (_standBy) return;
            _standBy = true;

        }


        protected override void OnActivate()
        {

            if (_standBy == false) _coordinates = new List<Coordinate>();

            _standBy = false;

            base.OnActivate();
        }


        private List<Coordinate> GetFourPt(List<Coordinate> Pt)
        {

            List<Coordinate> SquarePoints = new List<Coordinate>();
            SquarePoints.Add(_coordinates[0]);

            Coordinate p2, p3, p4;
            p2 = new Coordinate();
            p3 = new Coordinate();
            p4 = new Coordinate();



            p2.X = Pt[1].X;
            p2.Y = Pt[0].Y;

            p3.X = Pt[1].X;
            p3.Y = Pt[1].Y;

            p4.X = Pt[0].X;
            p4.Y = Pt[1].Y;

           
            SquarePoints.Add(p2);
            SquarePoints.Add(p3);
            SquarePoints.Add(p4);

            return SquarePoints;


        }


        protected override void OnDraw(MapDrawArgs e)
        {
            if (_standBy) return;  //如果本层不在编辑中，直接返回！！！

            //if (_parts != null)
            //{
            //    //------------------------------------------------
            //    List<Point> partPoints = new List<Point>();
            //    foreach (List<Coordinate> part in _parts)
            //    {
            //        foreach (Coordinate c in part)
            //        {
            //            partPoints.Add(Map.ProjToPixel(c));  //转换坐标
            //        }
            //        e.Graphics.DrawPolygon(Pens.Red, partPoints.ToArray());  //绘制矩形（多边形）
            //        partPoints.Clear();

            //    }

            //    Brush fill = new SolidBrush(Color.FromArgb(70, Color.LightCyan));
            //    e.Graphics.FillPolygon(fill, partPoints.ToArray());
            //    fill.Dispose();

            //    //这一部分是用来绘制和填充Part的，其实对我们来说 没有用
            //    //--------------------------------------------------

            //}

            //下面是绘制已经画的线和正在画的线

            Pen bluePen = new Pen(Color.Blue, 2F);
            List<PointF> points = new List<PointF>();
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            if (_coordinates != null && _coordinates.Count == 1)
            {

                List<Coordinate> Coor = new List<Coordinate>();
                Coor.Add(_coordinates[0]);
                Coor.Add(Map.PixelToProj(_mousePosition));

                List<Coordinate> SquarePt = new List<Coordinate>();
                SquarePt = GetFourPt(Coor);

                foreach (Coordinate c in SquarePt)
                {
                    points.Add(Map.ProjToPixel(c));

                }

                e.Graphics.DrawPolygon(bluePen, points.ToArray());

            }

            bluePen.Dispose();
            base.OnDraw(e);

        }


        protected override void OnMouseMove(GeoMouseArgs e)
        {
            if (_standBy) return;

            if (_coordinates != null && _coordinates.Count == 1)
            {

                List<Coordinate> Coor = new List<Coordinate>();
                Coor.Add(_coordinates[0]);
                Coor.Add(Map.PixelToProj(_mousePosition));

                List<Coordinate> SquarePt = new List<Coordinate>();
                SquarePt = GetFourPt(Coor);

                List<PointF> points = new List<PointF>();

                foreach (Coordinate c in SquarePt)
                {
                    points.Add(Map.ProjToPixel(c));

                }

                //左上角坐标
                PointF LeftTopPt = new PointF();
                LeftTopPt.X = Math.Min(points[0].X, points[2].X);
                LeftTopPt.Y = Math.Min(points[0].Y, points[2].Y);

                Rectangle invalid = new Rectangle(Convert.ToInt32(LeftTopPt.X), Convert.ToInt32(LeftTopPt.Y), Convert.ToInt32(Math.Abs(points[0].X - points[1].X)), Convert.ToInt32(Math.Abs(points[0].X - points[1].X)));
                invalid.Inflate(20, 20);
                Map.Invalidate(invalid);

            }

            _mousePosition = e.Location;
            base.OnMouseMove(e);

        }


        protected override void OnMouseUp(GeoMouseArgs e)
        {

            if (_standBy) return;

            if (_coordinates == null) _coordinates = new List<Coordinate>();

            if (e.Button == MouseButtons.Left)
            {
                if (_coordinates.Count < 2)
                {
                    _coordinates.Add(e.GeographicLocation);

                }
            }
            else
            {
                _coordinates.Clear();
                Map.Refresh();
            }

            if (_coordinates.Count == 2)
            {
                //最后一次擦除工作
                List<Coordinate> Coor = new List<Coordinate>();
                Coor.Add(_coordinates[0]);
                Coor.Add(Map.PixelToProj(_mousePosition));

                List<Coordinate> SquarePt = new List<Coordinate>();
                SquarePt = GetFourPt(Coor);

                List<PointF> points = new List<PointF>();

                foreach (Coordinate c in SquarePt)
                {
                    points.Add(Map.ProjToPixel(c));

                }

                //左上角坐标
                PointF LeftTopPt = new PointF();
                LeftTopPt.X = Math.Min(points[0].X, points[2].X);
                LeftTopPt.Y = Math.Min(points[0].Y, points[2].Y);

                Rectangle invalid = new Rectangle(Convert.ToInt32(LeftTopPt.X), Convert.ToInt32(LeftTopPt.Y), Convert.ToInt32(Math.Abs(points[0].X - points[1].X)), Convert.ToInt32(Math.Abs(points[0].X - points[1].X)));
                invalid.Inflate(20, 20);
                Map.Invalidate(invalid);

                FinishShape();

            }



        }


        int SquareID = 1;

        protected void FinishShape()
        {
            List<Coordinate> SquarePoints = new List<Coordinate>();

            SquarePoints = GetFourPt(_coordinates);

            foreach (IFeature f in (this.Parent.Parent.LayerSys.CurrentLayer.DataSet as IFeatureSet).Features)
            {
                SquareID = 1;
                if (SquareID <= int.Parse(f.DataRow["id"].ToString().Substring(1)))
                {
                    SquareID = int.Parse(f.DataRow["id"].ToString().Substring(1)) + 1;
                }
            }

            string id = "K" + SquareID.ToString();

            FSquareShape ss = new FSquareShape(SquarePoints.ToArray(), id);

            this.OnShapeAdded(ss);
            SquareID++;

            _coordinates = new List<Coordinate>();  //清空

            Map.Invalidate();

            //_coordinates = new List<Coordinate>();
            _parts = new List<List<Coordinate>>();



        }


        protected override void OnMouseWheel(GeoMouseArgs e)
        {
            base.OnMouseWheel(e);

            Rectangle r = e.Map.MapFrame.View;
            int w = r.Width;
            int h = r.Height;

            int scale = 5;

            if (e.Delta < 0)
            {
                r.Inflate(r.Width / (2 * scale), r.Height / (scale * 2));
                r.X += w / (2 * scale) - e.X / (scale);
                r.Y += h / (2 * scale) - e.Y / (scale);
                e.Map.MapFrame.View = r;
                e.Map.MapFrame.ResetExtents();
            }
            else
            {
                r.Inflate(-r.Width / (4 * scale), -r.Height / (4 * scale));
                // The mouse cursor should anchor the geographic location during zoom.
                r.X += (e.X / (2 * scale)) - w / (4 * scale);
                r.Y += (e.Y / (2 * scale)) - h / (4 * scale);
                e.Map.MapFrame.View = r;
                e.Map.MapFrame.ResetExtents();
            }

        }



        # endregion

    }
}
